package com.liuheqiao.sushegaunli.utils;

import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import com.liuheqiao.sushegaunli.model.Banner;
import com.liuheqiao.sushegaunli.model.Notice;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class JsonUtils {
    public static List<Banner> loadBannerData(Context context) {
        List<Banner> banners = new ArrayList<>();
        try {
            InputStream is = context.getAssets().open("banner_data.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String json = new String(buffer, StandardCharsets.UTF_8);

            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("banners");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                Banner banner = new Banner();
                banner.setId(obj.getInt("id"));
                banner.setImageUrl(obj.getString("imageUrl"));
                banner.setTitle(obj.getString("title"));
                banner.setContent(obj.getString("content"));
                banner.setTimestamp(obj.getString("timestamp"));
                banners.add(banner);
            }
        } catch (Exception e) {
            Log.e("JsonUtils", "Error loading banner data", e);
        }
        return banners;
    }

    public static List<Notice> loadNoticeData(Context context) {
        List<Notice> notices = new ArrayList<>();
        try {
            InputStream is = context.getAssets().open("notice_data.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String json = new String(buffer, StandardCharsets.UTF_8);

            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("notices");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                Notice notice = new Notice();
                notice.setId(obj.getInt("id"));
                notice.setTitle(obj.getString("title"));
                notice.setContent(obj.getString("content"));
                notice.setPublisher(obj.getString("publisher"));
                notice.setPublishTime(obj.getString("publishTime"));
                notice.setType(obj.getString("type"));
                
                // 处理图片数组
                List<String> images = new ArrayList<>();
                JSONArray imagesArray = obj.getJSONArray("images");
                for (int j = 0; j < imagesArray.length(); j++) {
                    images.add(imagesArray.getString(j));
                }
                notice.setImages(images);
                
                notices.add(notice);
            }
        } catch (Exception e) {
            Log.e("JsonUtils", "Error loading notice data", e);
        }
        return notices;
    }
} 